// stores/user.js
import {
	defineStore
} from 'pinia';
import {
	useSocketStore,
} from '@/stores/socket';
import {
	useContactsStore,
} from '@/stores/contacts';
import {
	useDynamicStore,
} from '@/stores/dynamic';
import {
	useChatStore,
} from '@/stores/chat';
import {
	userLoginApi,
	getUserInfoApi,
	getUserInfoByIdApi,
	putUserInfoApi,
	getCodeApi,
	registerAccountApi,
} from '@/api/user';
import {
	encrypt, // 加密
	decrypt, // 解密
} from '@/utils/crypto';

export const useUserStore = defineStore('user', {
	state: () => {
		return {
			token: '', // 令牌
			user: {}, // 用户详情
			userId: '', // 用户Id
			account: '', // 用户账号及手机号
			registerAccount: '', // 用户注册时，使用的账号及手机号
			registerSeconds: 0, // 用户注册时，获取验证码的时间戳
			resetPwdAccount: '', // 用户忘记密码时，使用的账号及手机号
			resetPwdSeconds: 0, // 用户注册时，获取验证码的时间戳
		};
	},
	// 也可以这样定义
	// state: () => ({ })
	actions: {
		// 用户登录
		async userLogin({
			userName,
			password,
		}) {
			return userLoginApi({
				userName,
				password: encrypt(password),
				isLogin: true,
			}).then(({
				data
			}) => {
				this.account = userName;
				this.token = data.data;
				return data;
			}).catch(e => Promise.reject(e));
		},
		// 用户注册
		async userRegister({
			userName,
			password,
			registerType,
		}) {
			return registerAccountApi({
				json: JSON.stringify({
					userName,
					password: encrypt(password),
					registerType,
				})
			}).then(({
				data,
				statusCode,
			}) => {
				this.account = userName;
				this.token = data.data;
				return {
					timestamp: data.timestamp,
				};
			}).catch(e => Promise.reject(e));
		},
		// 获取用户信息
		async getUserInfo() {
			if (this.userId) {
				let {
					data
				} = await getUserInfoByIdApi({
					id: this.userId,
				});
				this.user = data.data;
			} else {
				let {
					data
				} = await getUserInfoApi({
					token: this.token,
				});
				this.user = JSON.parse(decrypt(data.data));
			}
			this.userId = this.user.id;
		},
		// 修改用户信息
		async putUserInfo(params = {}) {
			try {
				let {
					data
				} = await putUserInfoApi({
					json: encrypt(JSON.stringify({
						userId: this.userId,
						...params
					})),
				});

				if (data.data.code == 1) {
					if (params.imgUrl) {
						this.user.avatarUrl = params.imgUrl;
					} else if (params.nickName) {
						this.user.nickName = params.nickName;
					} else if (params.gender) {
						this.user.gender = params.gender;
					} else if (params.address) {
						this.user.address = params.address;
					}
					return data.data;
				} else {
					throw Error(data.message)
				}
			} catch (e) {
				throw Error(e);
			}
		},
		// 获取短信验证码
		async getCode({
			phone,
			type,
		}) {
			return getCodeApi({
				phone,
				type,
			}).then(data => {
				switch (Number(type)) {
					case 1:
						this.registerAccount = phone;
						this.registerSeconds = data.data.timestamp;
						break;
					case 2:
						// this.registerAccount = phone;
						break;
					case 3:
						this.resetPwdAccount = phone;
						this.resetPwdSeconds = data.data.timestamp;
						break;
					default:
						break;
				}
				return data;
			}).catch(e => Promise.reject(e));
		}
	},
	unistorage: true // 开启后对 state 的数据读写都将持久化
});
